//
//  ToolClass.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/12.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "ToolClass.h"
#import "OrderedDictionary.h"


@implementation ToolClass

+ (CGSize)heighW:(UIFont *)font titleStr:(NSString *)title{
    CGSize titleSize = [title boundingRectWithSize:CGSizeMake(SCREEN_W-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font } context:nil].size;
    return titleSize;
}


//判断手机号码格式是否正确
+ (BOOL)valiMobile:(NSString *)mobile
{
    mobile = [mobile stringByReplacingOccurrencesOfString:@" "withString:@""];
//    if (mobile.length != 11)
//    {
//        return NO;
//    }else{
        /*固话：([0-9]{3,4}-)?[0-9]{7,8}
        手机：^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|18[02356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$
         */
        /**
         * 移动号段正则表达式
         */
        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
        /**
         * 联通号段正则表达式
         */
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
        /**
         * 电信号段正则表达式
         */
        NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
        
        NSString *GD_NUM= @"^([0-9]{3,4}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$";
        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
        BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
        BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
        BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
        NSPredicate *pred4 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", GD_NUM];
        BOOL isMatch4 = [pred4 evaluateWithObject:mobile];

        if (isMatch1 || isMatch2 || isMatch3||isMatch4) {
            return YES;
        }else{
            return NO;
        }
   // }
}


+ (NSMutableAttributedString *)AttributedString:(NSString *)title range:(NSRange )range color:(UIColor *)titlecolor{

    NSMutableAttributedString *strred = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",title]];
    //修改某个范围内字的颜色
    [strred addAttribute:NSForegroundColorAttributeName value:titlecolor  range:range];
    
    return strred;
}


+ (UIViewController *)viewController:(UIView *)weakself
{
    for (UIView *next = [weakself superview]; next; next = next.superview) {
        UIResponder *nextResponser = [next nextResponder];
        if ([nextResponser isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponser;
        }
    }
    return nil;
}


+ (NSString *)gettoken:(NSDictionary *)dicts{
    NSArray *keys = [dicts allKeys];
    NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    OrderedDictionary *dic = [[OrderedDictionary alloc]initWithCapacity:0];
    
    for(int i=0;i<sortedArray.count;i++){
        NSLog(@"====%@",dicts[sortedArray[i]]);
        [dic insertObject:dicts[sortedArray[i]] forKey:sortedArray[i] atIndex:i];
        
    }
    NSString *jsonStr = [self convertToJsonData:dic];
    
    NSString *token = [NSString stringWithFormat:@"%@**honglaba@2017**",jsonStr];
    // token = [self strToUnicode:token];
    
    NSLog(@"%@",token);
    
    token = [self md5:token];
    
    return token;
}

// 字典转json字符串方法

+(NSString *)convertToJsonData:(NSDictionary *)dict

{
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        
        NSLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    NSRange range3 = {0,mutStr.length};
    [mutStr replaceOccurrencesOfString:@"\\/" withString:@"/" options:NSLiteralSearch range:range3];
    return mutStr;
    
}

+ (NSString *) md5:(NSString *) input {
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return  output;
}

+ (NSString *)getData{
    NSDate *data = [NSDate date];
    NSString *date2 = [NSString stringWithFormat:@"%ld", (long)[data timeIntervalSince1970]];
    return date2;
}


+ (NSString *)strToTime:(NSString *)strTime format:(NSString *)foemat{
    NSString * time = strTime;//时间字符串
    NSInteger num = [time integerValue];//转为int型
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];//时间管理
    [formatter setDateStyle:NSDateFormatterMediumStyle];//时间方式
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:foemat];
    NSDate * confromTime = [NSDate dateWithTimeIntervalSince1970:num];
    NSString * comfromTimeStr = [formatter stringFromDate:confromTime];
    
    return comfromTimeStr;

}

//存图片
+ (BOOL)saveImage:(UIImage *)currenImage withName:(NSString *)name{
    NSData *imageData = UIImageJPEGRepresentation(currenImage, 0.5);
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:name];
   return [imageData writeToFile:fullPath atomically:NO];
}

//取图片
// 读取沙盒路径图片
+(UIImage *)getDocumentImage:(NSString *)pathName{
    
    NSString *aPath3= [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:pathName];
    // 拿到沙盒路径图片
    UIImage *imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:aPath3];
    
    return imgFromUrl3;
}

@end
